<?php

class MainControlPanel extends WBPanel { 
	
	private $_property = null;
	private $_projectManager = null;
	private $_styleList = array();
	private $_styleControl = null;
	private $_items = array(
		array('name' => 'pushbutton'),
		array('name' => 'imagebutton'),
		array('name' => 'panel'),
		//array('name' => 'invisiblearea'),
		array('name' => 'radiobutton'),
		array('name' => 'checkbox'),
		array('name' => 'hyperlink'),
		array('name' => 'label'),
		array('name' => 'editbox'),
		array('name' => 'rtfeditbox'),
		array('name' => 'frame'),
		array('name' => 'listbox'),
		array('name' => 'combobox'),
		array('name' => 'spinner'),
		array('name' => 'scrollbar'),
		array('name' => 'slider'),
		array('name' => 'gauge'),
		array('name' => 'calendar'),
		array('name' => 'htmlcontrol'),
		array('name' => 'listview'),
		array('name' => 'tabcontrol'),
		array('name' => 'treeview'),
	); 	
	
	private $_eventList = array(
		'click',		
		'resize',
		'redraw',
		'close',
		'mousedown',
		'mosuemove',
		'mouseup',
		'focus',
		'doubleclick',
		'dragstart',
		'drag',
		'dragend'
	);
	
	public function __construct($options = array()) { 
		parent::__construct($options);
	}
	
	public function initComponent() { 
		parent::initComponent();
		
		$this->createControl();
	}
	
	public function refreshProject() { 
		$this->getProjectManager()->refreshProject();
	}
	
	/**
	 * 
	 * @return ProjectManager
	 */
	public function getProjectManager() { 
		if (!$this->_projectManager) { 
			$this->_projectManager = wbobject('MainWindow')->getProjectManager();
		}
		
		return $this->_projectManager;
	}
	
	public function createControl() { 
		
		// set WBHBoxLayout 
		$this->setLayout('hbox');
		
		$this->createCheckBox();
		$this->addLabel(' + Project ');
		$this->createSourceTree();
		$this->addLabel(' + Controls ');
		$this->createControlButton();
		$this->addLabel(' + Properties ');				
		$this->createButton();
		$this->addBlank();		
		$this->createPropertyView();
		
	}	
	
	public function createControlButton() { 
		// source tree 
		$panel = $this->add(array('layout' => 'hbox', 'height' => 80));
		
		foreach ($this->_items as $i => $image) {

			if ($i % 11 == 0) { 
				$p1 = $panel->add(array('layout' => 'vbox', 'height' => 24));
			}
			
			$this->createControlImage($p1, $image['name']);
		}
	}
	
	public function createControlImage($panel, $caption) { 
			$image = RESOURCE_HOME."ctrl_${caption}3.bmp";
			
			$panel->add(array(
				'wbtype' => 'ImageButton',
				'width' => 24,
				'height' => 24,
				'caption' => $caption,
				'image' => $image,
				'event' => array(
					'click' => array($this, 'createSubControl')
				)
			));
	}
	
	public function createSubControl($eo) { 
		$text = $eo->object()->text;
		
		$this->getProjectManager()->createControl($text);		
	}
	
	public function createCheckBox() { 
		$panel = $this->add(array(
			'height' => 30, 
			'layout' => array('layout' => 'grid', 'columns' => 2, 'rows' => 1)
		));
		
		$panel->add(array(
			'wbtype' => 'CheckBox',
			'name' => 'checkSnapInGrid',		
			'caption' => 'Snap to grid',
			'checked' => true, 
			'event' => array('click' => array($this, 'checkStatus'))
		));
		
		$panel->add(array(
			'wbtype' => 'CheckBox',
			'name' => 'checkWireFrame',
			'caption' => 'wire frame',
			'checked' => true, 		
			'event' => array('click' => array($this, 'checkStatus'))
		));		
		
	}
	
	public function checkStatus($eo) { 
		$this->refreshProject();
	}
	
	public function createSourceTree() { 
		$tree = $this->add(new StructTree(array(
			'parent' => $this,
			'name' => 'StructTree',
			//'image' => RESOURCE_HOME."treeview.bmp",
			'width' => 200,
			'height' => 200
		)));		
		
		$tree->setImage(RESOURCE_HOME."treeview.bmp", GREEN, 1, 56);
		
	}
	
	public function viewProperty($eo) {
		$text = $eo->object()->text; 
		$obj = wbobject('PropertyPanel');
		if ($text == 'Properties') { 
			$obj->move(0);
		} else if ($text == 'Styles') { 
			$obj->move(1);			
		} else if ($text == 'Data') { 
			$obj->move(2);
		} else if ($text == 'Layout') { 
			$obj->move(3);			
		} else if ($text == 'Event') { 
			$obj->move(4);			
		} 
	}
	
	public function createButton() { 
		
		$panel = $this->add(array(
			'height' => 25,
			'layout' => 'button'
		));
		
		$event = array ( 'click' => array($this, 'viewProperty') );
		
		// combobox 
		$panel->add(array( 'wbtype' => 'PushButton', 'caption' => 'Properties', 'width' => 70, 'event' => $event ));
		$panel->add(array( 'wbtype' => 'PushButton', 'caption' => 'Styles', 'width' => 50, 'event' => $event ));
		$panel->add(array( 'wbtype' => 'PushButton', 'caption' => 'Data', 'width' => 50, 'event' => $event ));
		$panel->add(array( 'wbtype' => 'PushButton', 'caption' => 'Layout', 'width' => 50, 'event' => $event ));
		$panel->add(array( 'wbtype' => 'PushButton', 'caption' => 'Event', 'width' => 50, 'event' => $event ));		

	}
	
	public function createPropertyView() { 
		$panel = $this->add(array(
			'wbtype' => 'CardPanel',
			'name' => 'PropertyPanel',
			'height' => 250, 
		));
		
		$this->createPropertyList($panel);
		$this->createStyleList($panel);
		$this->createDataList($panel);
		$this->createLayoutList($panel);
		$this->createEventList($panel);
		
		$panel->move(0);
		
	}
	
	public function createStyleList($panel) { 
		$p1 = $panel->add(array(
			'wbtype' => 'ListView',
			'name' => 'StyleList',
			'checkbox' => true,
			'header' => array(array('Styles', 200)),
			'event' => array( 'click' => array($this, 'bindStyle'))
		));
	}
	
	public function setStyleListVisible($list = array()) {
		
		$this->_styleList = $list;

		// delete all items
		$style = wbobject('StyleList');
		$style->removeEvent('click', array($this, 'bindStyle'));
		$style->initItems();
		
		foreach ($list as $i => $obj) {
			
			$text = array($obj['text']);
			$style->addRow($text);

			$style->setChecked($i, $obj['value']);
		}

		$style->on('click', array($this, 'bindStyle'));		
		$style->click();
		
	}
	
	public function createPropertyList($panel) { 
		$p1 = $panel->add('spring columns=70,* rows=*,*,*,*,*,*,*,*,*,*');
		
		$this->createPropertyListToEditBox($p1, 'WBType :', 'wbtype');
		$this->createPropertyListToEditBox($p1, 'Name :', 'name');
		$this->createPropertyListToEditBox($p1, 'Title :', 'title');
		$this->createPropertyListToEditBox($p1, 'Caption :', 'caption');
		$this->createPropertyListToEditBox($p1, 'Text :', 'text');
		$this->createPropertyListToSpinner($p1, 'X : ', 'x', -3000, 3000);
		$this->createPropertyListToSpinner($p1, 'Y : ', 'y', -3000, 3000);
		$this->createPropertyListToSpinner($p1, 'Width : ', 'width', 0, 3000);
		$this->createPropertyListToSpinner($p1, 'Height : ', 'height', 0, 3000);
	}

	public function createDataList($panel) { 
		$p1 = $panel->add('full');
		
		$p1->add(array('wbtype' => 'EditBox', 'name' => 'items', 'multiline' => true, 'event' => array('click' => array($this, 'bindData'))));
		
	}	
	
	public function createLayoutList($panel) { 
		$p1 = $panel->add('spring columns=70,* rows=30,*');
		
		$p1->add(array('wbtype' => 'Label', 'caption' => 'Layout : ', 'align' => 'right'));
		$p1->add(array('wbtype' => 'EditBox', 'name' => 'layout', 'event' => array('click' => array($this, 'bindLayout'))));
		$p1->add(array('wbtype' => 'Label', 'caption' => 'Attributes : ', 'align' => 'right'));
		$p1->add(array('wbtype' => 'EditBox', 'name' => 'layoutAttribute', 'multiline' => true, 'event' => array('click' => array($this, 'bindLayout'))));
		
	}
	
	public function createEventList($panel) { 
		$p1 = $panel->add('spring columns=100,* rows=*,*,*,*,*,*,*,*,*,*,*,*');

		$this->createEventListToEditBox($p1, 'click(default) :', 'click');		
		$this->createEventListToEditBox($p1, 'resize :', 'resize');
		$this->createEventListToEditBox($p1, 'redraw :', 'redraw');
		$this->createEventListToEditBox($p1, 'close :', 'close');
		$this->createEventListToEditBox($p1, 'mousedown :', 'mousedown');
		$this->createEventListToEditBox($p1, 'mousemove :', 'mosuemove');
		$this->createEventListToEditBox($p1, 'mouseup :', 'mouseup');
		$this->createEventListToEditBox($p1, 'focus :', 'focus');
		$this->createEventListToEditBox($p1, 'doubleclick :', 'doubleclick');
		$this->createEventListToEditBox($p1, 'dragstart :', 'dragstart');
		$this->createEventListToEditBox($p1, 'drag :', 'drag');
		$this->createEventListToEditBox($p1, 'dragend :', 'dragend');
	}	
	
	/**
	 * 속성 리스트 데이타 전송 
	 * 
	 * @param unknown_type $eo
	 * @return unknown_type
	 */
	public function bind($eo) { 

		$obj = $eo->object();
		$name = $obj->getOption('name');
		$text = $obj->text;
		
		$data = array($name => $text);
	
		$this->updateProject($data);
	}
	
	/**
	 * 스타일 관련 데이타 전송 
	 * 
	 * @param unknown_type $eo
	 * @return unknown_type
	 */
	public function bindStyle($eo) { 
		$style = $eo->object();
		
		$len = $style->length();
		$data = array();
		for($i = 0; $i < $len; $i++) {
			$key = $this->_styleList[$i]['type'];
			$value = $style->isChecked($i); 
			$data[$key] = $value;
		}

		$this->updateProject($data);
	}
	
	/**
	 * items 관련 데이타 전송 
	 * 
	 * @param unknown_type $eo
	 * @return unknown_type
	 */
	public function bindData($eo) { 
		$this->updateProject(array(
			'items' => $eo->object()->text
		));
	}
	
	/**
	 * layout 관련 데이타 전송 
	 * 
	 * @param unknown_type $eo
	 * @return unknown_type
	 */
	public function bindLayout($eo) { 
		$layout = wbobject('layout')->text;
		$attribute = wbobject('layoutAttribute')->text;
		$attribute = str_replace(PHP_EOL, " ", $attribute);
		
		$layout .= " ".$attribute;
		
		$this->updateProject(array('layout' => $layout));
	}
	
	/**
	 * 이벤트 관련 데이타 전송 
	 * 
	 * @param unknown_type $eo
	 * @return unknown_type
	 */
	public function bindEvent($eo) {
		
		$arr = array();
		
		foreach ($this->_eventList as $event) { 
			$callback = wbobject('event_'.$event)->text;
			$arr[$event] = $callback;
		}
		
		$data = array( 'event' => $arr);
		
		$this->updateProject($data);
	}
	
	public function createEventListToEditBox($panel, $caption = '', $name = '') { 
		$panel->add(array( 'wbtype' => 'Label', 'caption' => $caption, 'align' => 'right' ));
		$editor = $panel->add(array( 'wbtype' => 'EditBox', 'caption' => '', 'name' => 'event_'.$name, 'event' => array('click' => array($this, 'bindEvent'))));
	}	
	
	public function createPropertyListToEditBox($panel, $caption = '', $name = '') { 
		$panel->add(array( 'wbtype' => 'Label', 'caption' => $caption, 'align' => 'right' ));
		$editor = $panel->add(array( 'wbtype' => 'EditBox', 'caption' => '', 'name' => $name, 'readOnly'=> ($name == 'wbtype'), 'event' => array('click' => array($this, 'bind'))));
	}
	
	public function createPropertyListToSpinner($panel, $caption = '', $name = '', $min = 0, $max = 0) { 
		$panel->add(array( 'wbtype' => 'Label', 'caption' => $caption, 'align' => 'right' ));
		$p1 = $panel->add(array(
			'layout' => array('layout' => 'spring', 'columns' => '* 20', 'rows' => 1)
		));
		
		$p1->add(array( 'wbtype' => 'EditBox', 'caption' => '0', 'name' => $name, 'event' => array('click' => array($this, 'bind'))));
		$p1->add(array( 'wbtype' => 'Spinner', 'group' => true, 'min' => $min, 'max' => $max ));
	}
	
	public function setControlValue($name, $value) { 
		wbobject($name)->text = $value;
	}
	
	public function updateControl($data, $isTranslate = false) {
		
		if ($data['dataType'] == 'window') {
			$this->updateWindow($data); 
			return ;
		}
		
		// update text field 
		$this->setControlValue('wbtype', 	$data['wbtype']['value']);
		$this->setControlValue('name', 		$data['name']['value']);
		$this->setControlValue('caption', 	$data['caption']['value']);
		$this->setControlValue('title', 	'');
		$this->setControlValue('text', 		$data['text']['value']);
		$this->setControlValue('x', 		$data['x']['value']);
		$this->setControlValue('y', 		$data['y']['value']);
		$this->setControlValue('width', 	$data['width']['value']);
		$this->setControlValue('height', 	$data['height']['value']);

		// update style
		if (!$isTranslate) { 
			$this->setCheckBoxList($data);
		}
		
		// data
		$items = wbobject('items');
		$items->disabled = false;
		$items->text = $data['items']['value'];		 
		
		// event
		$event = (array)$data['event']['value'];
		
		foreach ($event as $key => $value) { 
			wbobject('event_'.$key)->text = $value;
		}
		
		// layout
		$arr = explode(" ", (string)$data['layout']['value']);
		$layout = array_shift($arr);
		$attribute = implode(PHP_EOL, $arr);

		wbobject('layout')->text = $layout;
		wbobject('layoutAttribute')->text = $attribute;		
	}
	
	public function updateWindow($data, $isTranslate = false) { 

		// update text field 
		$this->setControlValue('wbtype', 	$data['wbtype']['value']);
		$this->setControlValue('name', 		$data['name']['value']);
		$this->setControlValue('caption', 	'');
		$this->setControlValue('title', 	$data['title']['value']);
		$this->setControlValue('text', 		'');
		$this->setControlValue('x', 		$data['x']['value']);
		$this->setControlValue('y', 		$data['y']['value']);
		$this->setControlValue('width', 	$data['width']['value']);
		$this->setControlValue('height', 	$data['height']['value']);
		
		// update style
		if (!$isTranslate) { 
			$this->setCheckBoxList($data);
		}

		// data 
		$items = wbobject('items');
		$items->disabled = true;
		$items->text = "";
				
		// event
		$event = (array)$data['event']['value'];
		
		foreach ($event as $key => $value) { 
			wbobject('event_'.$key)->text = $value;
		}
				
		// layout
		$arr = explode(" ", (string)$data['layout']['value']);
		$layout = array_shift($arr);
		$attribute = implode(PHP_EOL, $arr);

		wbobject('layout')->text = $layout;
		wbobject('layoutAttribute')->text = $attribute;
		
	}
	
	public function setCheckBoxList($data) {

		$list = array();
		foreach (FormControl::$checkboxControl as $type) { 
			if ($data[$type]['editor'] == 'checkbox') { 
				$list[] = array() + $data[$type];
			}
		}
		
		$this->setStyleListVisible($list);		
		
	}
	
	public function updateProject($data) {
		$project = $this->getProjectManager();
		if ($project) { 
			$project->updateProject($data);
		}
	}
	
}

?>